package com.cuckoo.cuckoo_app2.service;

import android.content.Context;

import android.os.Message;

import com.cuckoo.cuckoo_app2.MyApplication;
import com.cuckoo.cuckoo_app2.apiAsync.Imp.SiteLinkApiImp;
import com.cuckoo.cuckoo_app2.apiAsync.SiteLinkApi;
import com.cuckoo.cuckoo_app2.business.Imp.PlatformBusinessImp;
import com.cuckoo.cuckoo_app2.business.Imp.TipsMessagBusinessImp;
import com.cuckoo.cuckoo_app2.business.PlatformBusiness;
import com.cuckoo.cuckoo_app2.business.TipsMessagBusiness;
import com.cuckoo.cuckoo_app2.dao.Platform;
import com.cuckoo.cuckoo_app2.dao.TipsMessag;
import com.cuckoo.cuckoo_app2.model.IntentResponseModel;
import com.cuckoo.cuckoo_app2.model.JavaScriptResponseModel;
import com.cuckoo.cuckoo_app2.utils.LocalBroadCodeUtils;
import com.cuckoo.cuckoo_app2.utils.MessageCodeUtils;
import com.cuckoo.cuckoo_app2.utils.NotificationUtils;

import java.util.Date;
import java.util.Map;

/**
 * 后台消息处理
 */
public class MessageService {

    private Context context;

    public MessageService(Context context) {
        this.context = context;
    }

    /**
     * 接单任务 处理
     *
     * @param obj
     * @return
     */
    public boolean taskServeice(Object obj) {
        Map<String, Object> map = (Map<String, Object>) obj;
        JavaScriptResponseModel responseModel = (JavaScriptResponseModel) map.getOrDefault("model", null);
        Platform platform = (Platform) map.getOrDefault("data", null);
        MessageCodeUtils codeUtils = MessageCodeUtils.fromCode(Integer.parseInt(responseModel.getCode()));
        if (platform == null) {
            return false;
        }
        // 判断接单状态
        switch (codeUtils) {
            case SUCCESS:
                // 发送通知 修改通知信息 数据状态
                // 成功接单
                successTask(responseModel, platform);
                break;
            case WARNING:
                // 修改通知信息 修改登录状态
                // 服务器链接不上，登录失败
                warningTask(responseModel, platform);
                break;
            case FAIL:
                // 修改通知信息
                // 正在接单中
                failTask(responseModel, platform);
                break;
        }
        return true;
    }

    /**
     * 正在接单中 只修改通知信息
     *
     * @param responseModel
     * @param platform
     */
    private void failTask(JavaScriptResponseModel responseModel, Platform platform) {
        // 修改通知信息 数据状态 发送通知
        PlatformBusiness platformBusiness = new PlatformBusinessImp();
        TipsMessagBusiness tipsMessagBusiness = new TipsMessagBusinessImp();

        // 查询数据
        Platform selectPlatform = platformBusiness.getById(platform.getId());
        selectPlatform.setMessag(responseModel.getMessage());

        TipsMessag selectTipsMessag = tipsMessagBusiness.getById(selectPlatform.getId());
        boolean is_tips = true;
        if (selectTipsMessag == null) {
            is_tips = false;
            selectTipsMessag = new TipsMessag();
            selectTipsMessag.setId(selectPlatform.getId());
            selectTipsMessag.setTitle(selectPlatform.getTitle());
            selectTipsMessag.setImageUrl(selectPlatform.getImageUrl());
        }
        selectTipsMessag.setDate(new Date());
        selectTipsMessag.setMessag(responseModel.getMessage());
        selectTipsMessag.setStart(MessageCodeUtils.FAIL.getCode());

        // 更新数据
        platformBusiness.updata(selectPlatform);
        if (is_tips) {
            tipsMessagBusiness.updata(selectTipsMessag);
        } else {
            tipsMessagBusiness.install(selectTipsMessag);
        }

        // 通知页面 刷新
        LocalBroadCodeUtils.sendLocalBroadcast(context,
                LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH,
                new IntentResponseModel(LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH, null)
        );
    }

    /**
     * 服务器链接不上，登录失败 取消接单
     *
     * @param responseModel
     * @param platform
     */
    private void warningTask(JavaScriptResponseModel responseModel, Platform platform) {
        // 修改通知信息 数据状态 发送通知
        PlatformBusiness platformBusiness = new PlatformBusinessImp();
        TipsMessagBusiness tipsMessagBusiness = new TipsMessagBusinessImp();

        // 查询数据
        Platform selectPlatform = platformBusiness.getById(platform.getId());

        // 重新试3次 后再通知
        if (selectPlatform.getRetryCount() == null){
            selectPlatform.setRetryCount(0);
        }
        if (selectPlatform.getRetryCount().compareTo(3) < 0) {
            selectPlatform.setRetryCount(selectPlatform.getRetryCount() + 1);
            platformBusiness.updata(selectPlatform);
            return;
        }

        selectPlatform.setMessag(responseModel.getMessage());
        selectPlatform.setStart(0);
        selectPlatform.setRetryCount(0);

        TipsMessag selectTipsMessag = tipsMessagBusiness.getById(selectPlatform.getId());
        boolean is_tips = true;
        if (selectTipsMessag == null) {
            is_tips = false;
            selectTipsMessag = new TipsMessag();
            selectTipsMessag.setId(selectPlatform.getId());
            selectTipsMessag.setTitle(selectPlatform.getTitle());
            selectTipsMessag.setImageUrl(selectPlatform.getImageUrl());
        }
        selectTipsMessag.setDate(new Date());
        selectTipsMessag.setMessag(responseModel.getMessage());
        selectTipsMessag.setStart(MessageCodeUtils.WARNING.getCode());

        // 更新数据
        platformBusiness.updata(selectPlatform);
        if (is_tips) {
            tipsMessagBusiness.updata(selectTipsMessag);
        } else {
            tipsMessagBusiness.install(selectTipsMessag);
        }

        // 通知页面 刷新
        LocalBroadCodeUtils.sendLocalBroadcast(context,
                LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH,
                new IntentResponseModel(LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH, null)
        );

        // 发送通知
        NotificationUtils notificationUtils = MyApplication.getInstances().getNotificationUtils();
        notificationUtils.sendNotification(selectPlatform.getTitle(), responseModel.getMessage());
    }

    /**
     * 成功任务处理 只取消接单 等待完成订单后继续
     *
     * @param responseModel
     * @param platform
     */
    public void successTask(JavaScriptResponseModel responseModel, Platform platform) {
        // 修改通知信息 数据状态 发送通知
        PlatformBusiness platformBusiness = new PlatformBusinessImp();
        TipsMessagBusiness tipsMessagBusiness = new TipsMessagBusinessImp();

        // 查询数据 停止接单 防止提示被覆盖
        Platform selectPlatform = platformBusiness.getById(platform.getId());
        selectPlatform.setMessag(responseModel.getMessage());
        selectPlatform.setHasTips(1);

        TipsMessag selectTipsMessag = tipsMessagBusiness.getById(selectPlatform.getId());
        boolean is_tips = true;
        if (selectTipsMessag == null) {
            is_tips = false;
            selectTipsMessag = new TipsMessag();
            selectTipsMessag.setId(selectPlatform.getId());
            selectTipsMessag.setTitle(selectPlatform.getTitle());
            selectTipsMessag.setImageUrl(selectPlatform.getImageUrl());
        }
        selectTipsMessag.setDate(new Date());
        selectTipsMessag.setMessag(responseModel.getMessage());
        selectTipsMessag.setStart(MessageCodeUtils.SUCCESS.getCode());


        // 更新数据
        platformBusiness.updata(selectPlatform);
        if (is_tips) {
            tipsMessagBusiness.updata(selectTipsMessag);
        } else {
            tipsMessagBusiness.install(selectTipsMessag);
        }

        // 通知页面 刷新
        LocalBroadCodeUtils.sendLocalBroadcast(context,
                LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH,
                new IntentResponseModel(LocalBroadCodeUtils.LOCAL_BROADCAST_TASK_REFRESH, null)
        );

        // 发送通知
        NotificationUtils notificationUtils = MyApplication.getInstances().getNotificationUtils();
        notificationUtils.sendNotification(selectPlatform.getTitle(), responseModel.getMessage());
    }

}
